package com.iotechn.unimall.biz.service.coupon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.AppServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.data.domain.CouponDO;
import com.iotechn.unimall.data.domain.OrderDO;
import com.iotechn.unimall.data.domain.UserCouponDO;
import com.iotechn.unimall.data.enums.coupon.CouponType;
import com.iotechn.unimall.data.mapper.CouponMapper;
import com.iotechn.unimall.data.mapper.UserCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CouponBiz {

    @Autowired
    private UserCouponMapper userCouponMapper;

    @Autowired
    private CouponMapper couponMapper;

    public void userCoupon(OrderDO order){
        List<UserCouponDO> userCouponDOs = userCouponMapper.selectList(new EntityWrapper<UserCouponDO>().eq("order_id", order.getId()));
        UserCouponDO userCouponDO=new UserCouponDO();
        if(userCouponDOs != null){
            userCouponDO = userCouponDOs.get(0);
            userCouponDO.setGmtUsed(new Date());
            userCouponMapper.updateById(userCouponDO);
        }
    }


    /**
     * 领取优惠券
     * @param userId
     * @param couponDO
     * @throws AdminServiceException
     */
    public void userReceiveCoupon(Long userId,CouponDO couponDO) throws AppServiceException {
        Date now= new Date();
        //新人优惠券不受张数限制
        if(couponDO.getType().intValue() == CouponType.NEW_USER_COUPON.getCode()){
            //领取时手动添加张数
            couponDO.setSurplus(couponDO.getSurplus() + 1);
        }
        //校验优惠券剩余张数是否可领
        if(couponDO.getSurplus().intValue() < 1){
            throw new AppServiceException("优惠券剩余不足",500);
        }
        //校验优惠券每人限领张数
        List<UserCouponDO> userCouponDOS = userCouponMapper.selectList(new EntityWrapper<UserCouponDO>().eq("user_id", userId).eq("coupon_id", couponDO.getId()));
        if(couponDO.getLimit().intValue() <= userCouponDOS.size()){
            throw new AppServiceException("该优惠券单人领取已到上限",500);
        }
        //优惠券领取
        //领取优惠券
        UserCouponDO userCouponDO = new UserCouponDO();
        userCouponDO.setUserId(userId);
        userCouponDO.setCouponId(couponDO.getId());
        if (couponDO.getGmtStart() != null && couponDO.getGmtEnd() != null) {
            //如果优惠券是按区间领取的
            userCouponDO.setGmtStart(couponDO.getGmtStart());
            userCouponDO.setGmtEnd(couponDO.getGmtEnd());
        } else if (couponDO.getDays() != null) {
            //如果是任意领取的，则从当前时间 加上 可用天数
            userCouponDO.setGmtStart(now);
            userCouponDO.setGmtEnd(new Date(now.getTime() + 1000l * 60 * 60 * 24 * couponDO.getDays()));
        } else {
            throw new AppServiceException(ExceptionDefinition.COUPON_STRATEGY_INCORRECT);
        }
        userCouponDO.setGmtUpdate(now);
        userCouponDO.setGmtCreate(now);
        userCouponMapper.insert(userCouponDO);
    }
}
